﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using SqliteSugar;
using Seed.StockHandLine.Dao;
using Seed.StockHandLine.Models;

namespace Seed.StockHandLine.Reposities
{
    public class LongkeCodeResposities
    {
        public bool IsAnyBigCode(string code)
        {
            using (var db = SugarDao.GetInstance())
            {
                return db.Queryable<LongkeCode>().Any(it => it.BigCode == code);
            }
        }

        public bool IsAnySmlCode(string code)
        {
            using (var db = SugarDao.GetInstance())
            {
                return db.Queryable<LongkeCode>().Any(it => it.SmlCode == code);
            }
        }

        public void InsertCodes(string code, ListBox smlCodeList, LongkeCode longke, int productNubs)
        {
            var day = string.Format("{0:yyyyMMdd}", DateTime.Now);
            using (var db = Dao.SugarDao.GetInstance())
            {
                var lists = (from string sCode in smlCodeList.Items
                    select new LongkeCode
                    {
                        BigCode = code,
                        SmlCode = sCode,
                        Patch = longke.Patch,
                        ProductName = longke.ProductName,
                        SaleInfo = longke.SaleInfo,
                        BoxNubs = longke.BoxNubs,
                        ProductLine = longke.ProductLine
                    }).ToList();
                db.InsertRange(lists);
                db.Update<ProductCount>(new {Counts = productNubs}, it => it.Days == day);
            }
        }

        public void InsertCodes(List<SaleCode> list)
        {
            using (var db = SugarDao.GetInstance())
            {
                db.InsertRange(list);
            }
        }

        public int Counts()
        {
            string day = string.Format("{0:yyyyMMdd}", DateTime.Now);
            using (var db = SugarDao.GetInstance())
            {
                // 今天已包装件数
                ProductCount pc = db.Queryable<ProductCount>().Where(it => it.Days == day).FirstOrDefault();
                if (pc != null)
                {
                   return pc.Counts;
                }
                else
                {
                    db.Insert(new ProductCount
                    {
                        Counts = 0,
                        Days = day
                    });
                    return 0;
                }
            }
        }

        public LongkeCode Last()
        {
            using (var db = SugarDao.GetInstance())
            {
                return db.Queryable<LongkeCode>().OrderBy("Id desc").FirstOrDefault();
            }
        }
        /// <summary>
        /// 每次就上传5万条记录吧
        /// </summary>
        /// <param name="maxId"></param>
        /// <returns></returns>
        public List<LongkeCodes> CodeUpdate(int maxId)
        {
            using (var db = SugarDao.GetInstance())
            {
                return db.Queryable<LongkeCode>().Where(it => it.Id > maxId).OrderBy("Id")
                    .Take(50000).ToList()
                .Select(it => new LongkeCodes
                {
                    BigCode = it.BigCode,
                    SmlCode = it.SmlCode,
                    Patch = it.Patch,
                    ProductName = it.ProductName,
                    SaleInfo = it.SaleInfo,
                    BoxNubs = it.BoxNubs,
                    ProductLine = it.ProductLine,
                    ProductTime=it.ProductTime,
                    Status = it.Id.ObjToString()
                }).ToList();
            }
        }

 

        public List<SaleCodes> SaleUpdate(int maxId)
        {
            using (var db = SugarDao.GetInstance())
            {
                return db.Queryable<SaleCode>().Where(it => it.Id > maxId).ToList().Select(it => new SaleCodes
                {
                    BigCode=it.BigCode,
                    SaleInfo=it.SaleInfo,
                    CreateTime=it.CreateTime,
                    ProductLine=it.ProductLine,
                    Status=it.Id.ToString()
                }).ToList();
            }
        }
    }
}
